/**
 * Licensed under the MIT License
 *
 * Copyright (c) 2010 Alexandre Croiseaux
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package marcel.display.gui.components
{
	import flash.display.Shape;
	import flash.geom.Point;
	import marcel.display.draw.DrawManager;
	import marcel.display.draw.props.FillProps;
	import marcel.display.draw.props.PolygonProps;
	import marcel.display.gui.buttons.ColorButton;
	import marcel.display.gui.texts.CoreField;
	
	/**
	 * ListItemRenderer class used to display items in a list or a combobox. This class can be edited directly to change graphics.
	 * @author Alexandre Croiseaux
	 */
	public class ListItemRenderer extends ColorButton
	{
		//--------------------------------------------------------------------------
		//
		//  Public vars
		//
		//--------------------------------------------------------------------------
		/**
		 * Indicates the index of the item when put into a list
		 */
		public var index:uint;
		
		
		//--------------------------------------------------------------------------
		//
		//  Private vars
		//
		//--------------------------------------------------------------------------
		/**
		 * @private
		 */
		protected var _label:CoreField;
		/**
		 * @private
		 */
		protected var _bg:Shape;
		/**
		 * @private
		 */
		protected var _selected:Boolean;
		/**
		 * @private
		 */
		protected var _arrow:Shape;
		/**
		 * @private
		 */
		protected var _arrowColor:uint;
		/**
		 * @private
		 */
		protected var _arrowColorOver:uint;
		/**
		 * @private
		 */
		protected var _tweenDuration:Number;
		/**
		 * @private
		 */
		protected var _bgColor:uint;
		/**
		 * @private
		 */
		protected var _bgColorOver:uint;
		/**
		 * @private
		 */
		protected var _textColor:uint;
		/**
		 * @private
		 */
		protected var _textColorOver:uint;
		/**
		 * @private
		 */
		protected var _textCSSClass:String;
		
		
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		/**
		 * Constructs a new ListItemRenderer instance
		 */
		public function ListItemRenderer()
		{
			initColors();
			
			_label = new CoreField();
			_label.setCSSStyle(_textCSSClass);
			_label.mouseEnabled = false;
			_label.x = 5;
			_label.y = 1;
			registerColor(_label, _textColorOver, _textColor, _tweenDuration);
			addChild(_label);
			
			_bg = new Shape();
			_bg.graphics.beginFill(0x000000, 1);
			_bg.graphics.drawRect(0, 0, 100, _label.height + 4);
			registerColor(_bg, _bgColorOver, _bgColor, _tweenDuration);
			addChildAt(_bg, 0);
			
			setOut();
		}
		
		/**
		 * Defines the diferrents colors for the text and backgrousn on rollover/rollout
		 */
		public function initColors():void
		{
			/////// Change values to modify display ///////////////////////
			_bgColor = 0xD4D4D4;
			_bgColorOver = 0xFF6600;
			_textColor = 0x333333;
			_textColorOver = 0xFFFFFF;
			_textCSSClass = "list_item";
			_tweenDuration = .5;
			_arrowColor = _textColor;
			_arrowColorOver = _textColorOver;
			//////////////////////////////
		}
		
		/**
		 * Updates the width of the current item
		 * @param	w	the width in pixels
		 */
		override public function set width(w:Number):void
		{
			_bg.width = w;
			_label.width = w - 5;
			if (_arrow) _arrow.x = _bg.width - _arrow.width - 5;
		}
		
		/**
		 * Returns the height if the current item. This method is used by the owner list to calculate the position of the items
		 * @return
		 */
		override public function get height():Number
		{
			return _bg.height;
		}
		
		/**
		 * Set the current item as selected or not
		 * @param	bSelected	true to select the current item, false otherwise.
		 * @param	lockIfSelected	true to lock the item on selection, false otherwise.
		 */
		public function setSelected(bSelected:Boolean, lockIfSelected:Boolean = true):void
		{
			_selected = bSelected;
			_selected ? setOver() : setOut();
			if (lockIfSelected) lock(bSelected);
			else
			{
				lockRoll(bSelected);
				lock(false);
			}
		}
		
		/**
		 * Set the text to display in the current item
		 * @param	str	A string text
		 */
		public function setLabel(str:String):void
		{
			_label.htmlText = str;
		}
		
		/**
		 * Set the data in the current item
		 * @param	data	A data object
		 */
		public function setData(data:Object):void
		{
		}
		
		/**
		 * Transforms this item into a combobox header. override or modify this method to edit the graphics of the combobox header
		 */
		public function transformToComboHeader():void
		{
			// reduce label
			_label.width = _bg.width - 25;
			
			// create arrow
			_arrow = new Shape();
			DrawManager.drawPolygon(_arrow, new PolygonProps(new Point(0, 0), new Point(10, 0), new Point(5, 8)), true, new FillProps(_arrowColor));
			
			// register arrow for rollover effect
			registerColor(_arrow, _arrowColorOver, _arrowColor, _tweenDuration);
			
			// display aeeow
			addChildWithProps(_arrow, {x:_bg.width - _arrow.width - 5, y:6});
		}
		
	}

}